#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/6/15 
# @Author  : Mik
import requests
import json as js
from common.logger import logger
from common.read_config import load_ini


class BaseRequest:
    def __init__(self, method, url, params_type, header=None, file=None, **kwargs):
        self.method = method
        self.url = load_ini('host')['root_url'] + url
        self.params_type = params_type
        self.header = header
        self.file = file
        self.s = requests.Session()
        self.kwargs = kwargs

    def send_request(self, ids=None, header=None, data=None):
        if ids is not None:
            url = self.url + ids

        else:
            url = self.url

        if header is not None:
            headers = self.header.update(header)

        else:
            headers = self.header

        if data is not None:
            datas = data

        else:
            datas= None



        if self.params_type == 'params':
            res = self.s.request(
                method=self.method,
                url=url,
                params=datas,
                headers=headers)
        elif self.params_type == 'data':
            res = self.s.request(
                method=self.method,
                url=url,
                data=datas,
                files=self.file,
                headers=headers)
        elif self.params_type == 'json':

            res = self.s.request(
                method=self.method,
                url=url,
                json=datas,
                files=self.file,
                headers=headers)
        else:
            raise ValueError(
                '可选关键字为params, json, data')

        return res
